﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace TestGenerator
{
    class QuestionData
    {
        const string openTag = "<p class=";
        const string closeTag = "</p>";
        const string questionKey = "Question>Question. ";
        const string correctKey = "CorrectAnswer>Correct. ";
        const string wrongKey = "WrongAnswer>Wrong. ";
        public string header;
        public string footer;
        public List<Question> Questions { get; private set; }

        public QuestionData(string fileName)
        {
            Questions = new List<Question>();
            using (FileStream fileStream = new FileInfo(fileName).OpenRead())
            {
                using (StreamReader reader = new StreamReader(fileStream, Encoding.GetEncoding(1251)))
                {
                    string text = reader.ReadToEnd();
                    string[] classes = Regex.Split(text, openTag);
                    string currentQuestionText = null;
                    List<string> WA = new List<string>();
                    List<string> CA = new List<string>();
                    GetFooterAndHeader(text, out footer, out header);
                    for (int i = 0; i < classes.Length; i++)
                    {
                        classes[i] = classes[i].TrimEnd();
                        if (classes[i].StartsWith(questionKey))
                        {
                            if (!String.IsNullOrEmpty(currentQuestionText))
                            {
                                Questions.Add(new Question(currentQuestionText, WA, CA));
                                WA = new List<string>();
                                CA = new List<string>();
                            }
                            currentQuestionText = GetContent(classes[i], questionKey);
                        }
                        else if (classes[i].StartsWith(correctKey))
                        {
                            string correctAnswer = GetContent(classes[i], correctKey);
                            CA.Add(correctAnswer);
                        }
                        else if (classes[i].StartsWith(wrongKey))
                        {
                            string wrongAnswer = GetContent(classes[i], wrongKey);
                            WA.Add(wrongAnswer);
                        }
                    }

                    if (!String.IsNullOrEmpty(currentQuestionText))
                    {
                        Questions.Add(new Question(currentQuestionText, WA, CA));
                        WA = new List<string>();
                        CA = new List<string>();
                    }
                }
            }
        }

        private static void GetFooterAndHeader(string text,out string footer,out string header)
        {
            header = text.Substring(0, text.IndexOf(questionKey, 0)-9);
           int lastIndex=Math.Max( text.LastIndexOf(correctKey),Math.Max(text.LastIndexOf(wrongKey),text.LastIndexOf(correctKey)));
           lastIndex = text.IndexOf(closeTag, lastIndex);
           footer = text.Substring(lastIndex + closeTag.Length, text.Length - lastIndex - closeTag.Length);
        }
        private static string GetContent(string raw, string tag)
        {
            string content = raw.Substring(tag.Length, raw.Length - closeTag.Length - tag.Length);
            return content;
        }

    }
}

